package com.yxj08;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFileExceptionDemo{
    //JDK9
    private static void methode4() throws IOException {
        FileReader fr = new FileReader("fr.txt");
        FileWriter fw = new FileWriter("fw.txt");
        try(fr;fw) {
            char[] chs = new char[1024];
            int len;
            while((len = fr.read()) != -1) {
                fw.write(chs, 0, len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    //JDK7
    private static void methode3() {
        try(FileReader fr = new FileReader("fr.txt");
            FileWriter fw = new FileWriter("fw.txt");) {
            char[] chs = new char[1024];
            int len;
            while((len = fr.read()) != -1) {
                fw.write(chs,0,len);
            }catch(IOException e) {
                e.printStackTrace();
            }

        }

    }
    //try catch finally
    private static void methode2() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("fr.txt");
            fw = new FileWriter("fw.txt");
            char[] chs = new char[1024];
            int len;
            while((len = fr.read()) != -1) {
                fw.write(chs,0,len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fr!=null) {
                try {
                    fr.close();
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
           if(fw != null) {
               try {
                   fw.close();
               }catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
    //抛出异常
    private static void methode1() throws IOException {
        FileReader fr = new FileReader("fr.txt");
        FileWriter fw = new FileWriter("fw.txt");
        char[] chs = new char[1024];
        int len;
        while((len = fr.read()) != -1) {
            fw.write(chs,0,len);
        }
        fr.close();
        fw.close();
    }
}
